precision mediump float;

varying vec3 FragPos;
varying vec3 Normal;
varying vec3 LightPos;   // Extra in variable, since we need the light position in view space we calculate this in the vertex shader

uniform vec3 viewPos;
uniform vec3 lightColor;
uniform vec3 objectColor;

void main()
{
    // Ambient
    float ambientStrength = 0.1;
    vec3 ambient = ambientStrength * lightColor;

    // Diffuse
    vec3 norm = normalize(Normal);
    // 计算光源和片段位置之间的方向向量。前面提到，光的方向向量是光的位置向量与片段的位置向量之间的向量差。
    vec3 lightDir = normalize(LightPos - FragPos);
    // 对norm和lightDir向量进行点乘，来计算光对当前片段的实际的散射影响。结果值再乘以光的颜色，得到散射因子。两个向量之间的角度越大，散射因子就会越小
    float diff = max(dot(norm, lightDir), 0.0);
    vec3 diffuse = diff * lightColor;

    // Specular
    // 定义一个镜面强度(Specular Intensity)变量specularStrength，给镜面高光一个中等亮度颜色，这样就不会产生过度的影响了
    //如果把它设置为1.0f，会得到一个对于模型度明亮的镜面亮度因子。
    float specularStrength = 0.5;
    vec3 viewDir = normalize(viewPos - FragPos);
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    vec3 specular = specularStrength * spec * lightColor;

    vec3 result = (ambient + diffuse + specular) * objectColor;
    gl_FragColor = vec4(result, 1.0);
}